<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_fctrl_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/forms/vpage_system_attribute/vpage_system_attribute_form_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_system_attribute_dbc.class.php');


class VPage_System_Attribute_Edit_WP extends TCMS_Admin_Page_W_FCtrl_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_vpage/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('vpage');
		$this->add_transit_var('category');
	}


	public function init() {
		parent::init();

		$dbc = $this->get_dbc();
		$vpage = new TCMS_VPage_DBC();
		if ($vpage->load_by_id($dbc, $this->get_transit_var('vpage'))) {
			$this->export('page_path', $vpage->get_path());
		}

		$context = $this->get__context();
		if ($context->exists_in_get('vpt_sa')) {
			$vpt_sa_id = $context->get_from_get('vpt_sa');
			$vpt_sa = new TCMS_VPT_System_Attribute_DBC();
			if ($vpt_sa->load_by_id($dbc, $vpt_sa_id)) {
				$this->export('vpt_sa_name', $vpt_sa->get_sa_name());
			}
		} else {
			throw new Tangra_Module_Exception('Expecting vpt_sa parameter.');
		}
	}


	protected function create_form_ctrl() {
		$dbc = $this->get_dbc();
		$tvm = $this->get_tvm();

		$ret = new VPage_System_Attribute_Form_Ctrl('vpage_system_attribute', $tvm, $dbc, 'obj_id');

		return $ret;
	}


	protected function on_add_new() {
		$context = $this->get__context();
		$this->form_ctrl->set_permanent_external_param('vpt_sa', $context->get_from_get('vpt_sa'));

		$this->form_ctrl->set_permanent_external_param('vpage', $this->get_transit_var('vpage'));
		$this->export('vpage_id', $this->get_transit_var('vpage'));


		$current_user = $this->get_user();
		$this->form_ctrl->set_permanent_external_param('user', $current_user->get_id());
	}


	protected function on_received_id($object_id) {
		$this->form_ctrl->set_permanent_external_param('vpage', $this->get_transit_var('vpage'));
		$this->export('vpage_id', $this->get_transit_var('vpage'));

		$current_user = $this->get_user();
		$this->form_ctrl->set_permanent_external_param('user', $current_user->get_id());
	}


	protected function create_default_redir_view() {
		$object = $this->form_ctrl->get_object();
		$redir_view = $this->create_redirect_view($this->get_redir_on_success(), array('manage' => $object->get_id()));

		return $redir_view;
	}
}